package com.java1412.validator;

import com.java1412.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * @author java1412
 * @create 2021-05-14 15:29
 */
@Component
public class EmailValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return User.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        User user = (User) target;
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "", "邮箱为空");
        if (!user.getEmail().contains("@")) {
            errors.rejectValue("email", "", "邮箱不是有效的");
        }
    }

}